package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.opensymphony.xwork2.Result;

import henu.bean.User;
import henu.dao.IUserDao;
/**
 * @author 纪柳任
 * 2016年9月19日下午8:25:33
 */
public class UserDaoImpl implements IUserDao{

	QueryRunner queryRunner= new QueryRunner();
//	protected QueryRunner queryRunner = new QueryRunner();
//rows是用来测试的返回行数
	private int rows;

	public UserDaoImpl(QueryRunner queryRunner) {
		super();
		this.queryRunner = queryRunner;
	}

	@Override
	public void add(User user) {
	
		String sql = "insert into tb_user(fUsername,fRealname,fPassword,"
				+ "fReminderQuestion,fReminderAnswer,fUserType,fRemark) values(?,?,?,?,?,?,?)";
		Object[] param ={user.getfUsername(),user.getfRealname(),user.getfPassword(),user.getfReminderQuestion(),user.getfReminderAnswer(),
				user.getfUserType(),user.getfRemark()};
		try {
			rows=queryRunner.update(sql, param);
        System.out.println(rows+"行已经发生变化");
      
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
	}

	@Override 
	public void delete(String fUsername) {
		// TODO Auto-generated method stub
	
		String sql="delete from tb_user where fUsername=?";
		Object[] param={fUsername};
		try {
			rows=queryRunner.update(sql,param);
			System.out.println(rows+"行已经发生变化");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public List<User> querry(String province,String values) {
		String sql1 = "select * from tb_user where fRealname=?";
		String sql2 = "select * from tb_user where fUsername=?";
		String sql3 = "select * from tb_user where fUserType=?";
		Object param[]={values};
		List<User> users= null;
		if(province.equals("按姓名"))
		{

			try {
				
			users =queryRunner.query(sql1, param,new BeanListHandler<User>(User.class));
			System.out.println("执行完毕");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(province.equals("按用户名"))
		{

			try {
				users = queryRunner.query(sql2, param,new BeanListHandler<User>(User.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if(province.equals("按用户类型"))
		{

			try {
				users = queryRunner.query(sql3, param,new BeanListHandler<User>(User.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return users;
		
	}
	

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		System.out.println(user.getfUsername());
		String sql="update tb_user set fRealname=?,fPassword=?,fReminderQuestion=?,fReminderAnswer=?,fUserType=?,fRemark=? where fUsername=?";
		Object param[]={user.getfRealname(),user.getfPassword(),user.getfReminderQuestion(),user.getfReminderAnswer(),user.getfUserType(),user.getfRemark(),user.getfUsername()};
		try {
			
			rows=queryRunner.update(sql,param);
			System.out.println(rows+"行已经发生变化");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public List<User> getAll() {

		String sql = "select * from tb_user";
		List<User> users = null;		
		try {
			users = queryRunner.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return users;
	}

	@Override
	public User get(String fUsername) {
		// TODO Auto-generated method stub
		String sql="select * from tb_user where fUsername=?";
		Object param[]={fUsername};
		User user=null;
		try {
			user=queryRunner.query(sql, param, new BeanHandler<User>(User.class));
			System.out.println("get");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public int updatePwd(String username , String newPwd) {
		String sql = "update tb_user set fPassword = ?  where fUserName = ?";
		Object[] param = {newPwd,username};
		int n = 0;
		try {
			n = queryRunner.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
		
	}
	
	
	
}
